@echo off
set APP_NAME=BootApplication-1.1-SNAPSHOT.jar
cd %~dp0

rem 使用说明，用来提示输入参数
:usage
echo Usage: %0 [start|stop|restart|status]
exit /b 1

rem 检查程序是否在运行
:is_exist
for /f "tokens=2" %%i in ('tasklist ^| findstr /i "%APP_NAME%"') do set pid=%%i
if "%pid%"=="" (
  exit /b 1
) else (
  exit /b 0
)

rem 启动方法
:start
call :is_exist
if %errorlevel% equ 0 (
  echo %APP_NAME% is already running. pid=%pid%.
) else (
  start /B javaw -jar %APP_NAME% --spring.profiles.active=prod --server.port=8083 > nohup.log 2>&1
  echo %APP_NAME% is start success
)

rem 停止方法
:stop
call :is_exist
if %errorlevel% equ 0 (
  taskkill /F /PID %pid%
  echo %APP_NAME% is stopped.
) else (
  echo %APP_NAME% is not running.
)

rem 输出运行状态
:status
call :is_exist
if %errorlevel% equ 0 (
  echo %APP_NAME% is running. Pid is %pid%
) else (
  echo %APP_NAME% is NOT running.
)

rem 重启
:restart
call :stop
call :start

rem 根据输入参数，选择执行对应方法，不输入则执行使用说明
if "%1"=="start" (
  call :start
  type nohup.log
) else if "%1"=="stop" (
  call :stop
) else if "%1"=="status" (
  call :status
) else if "%1"=="restart" (
  call :restart
  type nohup.log
) else (
  call :restart
  type nohup.log
)
